from django.db import models
from django.contrib.auth.models import User

# Create your models here.

class Recipe(models.Model):
    title = models.CharField(default='New Recipe', max_length=100)
    file = models.CharField(default='filename', max_length=100)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    add_time = models.DateTimeField(auto_now_add=True)


class Feed(models.Model):
    title = models.CharField(default='New Feed', max_length=100)
    type = models.CharField(default='r', max_length=10) # r: RSS b: BookRecipe
    url = models.CharField(default='example.com', max_length=500)
    available = models.BooleanField(default=True)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    public = models.BooleanField(default=False)


class Book(models.Model):
    title = models.CharField(default='New Book', max_length=100)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    type = models.CharField(default='f', max_length=2) # f: Feed r Recipe
    feeds = models.ManyToManyField('Feed')
    recipe = models.ForeignKey('Recipe', on_delete=models.CASCADE, null=True)

    # Part of Cron Expression
    # Min Hour WeekDay MonthDay
    #
    # EXAMPLES
    #
    # 每日0点：0 0 * *
    # 每周一0点：0 0 1 *
    # 每月1日: 0 1 * 1

    fetch_hour = models.IntegerField(default=-1)
    fetch_min = models.IntegerField(default=-1)
    fetch_weekday = models.CharField(default='*', max_length=1)
    fetch_monthday = models.CharField(default='*', max_length=1)


class UserFeed(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    feed = models.ForeignKey(Feed, on_delete=models.CASCADE)


